import os
from box import Box
from loguru import logger


class ConfigBox:
    file_mapping = {
        ".yaml": Box.from_yaml,
        ".json": Box.from_json,
    }

    def __new__(cls, path, *args, **kwargs):
        try:
            suffixes = os.path.splitext(path)[-1]
        except Exception as error:
            logger.error(f"file error {path}, msg: {error}")
            return
        function = cls.file_mapping.get(suffixes)
        if not function:
            logger.error(f"not support file type {suffixes}, current file: {path}")
            return
        logger.debug(f"loading config from {path}")
        return function(filename=path)

    def set_env(self, name):
        if name in os.environ:
            logger.warning(f"The variable name already exists")
            return
        os.environ[name] = self

    @classmethod
    def get_env(cls, name):
        return os.environ.get(name, {})


"""

"""
